package com.example.gty.service.Impl;

import com.example.gty.entity.*;
import com.example.gty.mapper.CourseMapper;
import com.example.gty.mapper.GradeMapper;
import com.example.gty.mapper.TeacherCourseMapper;
import com.example.gty.service.CourseService;
import com.example.gty.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TeacherCourseMapper teacherCourseMapper;
    @Autowired
    private GradeMapper gradeMapper;

    //查找课程及任课老师
    public List<CourseVo> selectCourseAll() {
        return courseMapper.selectAllCourse();
    }

    //批量添加课程及任课老师
    public ResultVo addMultiCourse(List<CourseVo1> list) {
        int insert=0,insert1=0;
        //判断课程是否已存在
        for (int i = 0; i < list.size(); i++) {
            CourseVo1 courseVo1 = list.get(i);
            Course course = courseMapper.selectByPrimaryKey(courseVo1.getCno());
            if (course == null) {
                //添加课程信息
                Course course1 = new Course(courseVo1.getCno(), courseVo1.getTerm(), courseVo1.getCname(), courseVo1.getCcredit(), courseVo1.getChour(), new Date());

                insert = courseMapper.insert(course1);
                //查看老师课程信息
                Example example = new Example(TeacherCourse.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("tno", courseVo1.getTno());
                criteria.andEqualTo("cno", courseVo1.getCno());
                List<TeacherCourse> list1 = teacherCourseMapper.selectByExample(example);
                if (list1 == null || list1.size() == 0) {
                    //添加老师课程信息
                     insert1 = teacherCourseMapper.insert(new TeacherCourse(courseVo1.getTno(), courseVo1.getCno()));

                }

            }
        }
        if (insert > 0 && insert1 > 0) {
            return new ResultVo(1000, "success", null);
        }
        return new ResultVo(1001, "error", null);
    }

    //删除课程
    public ResultVo deleteCourse(String cno) {
        //成绩表
        Example example = new Example(Grade.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cno",cno);
        List<Grade> grades = gradeMapper.selectByExample(example);
        if (grades==null||grades.size()==0){
            //删除课程
            int i1 = courseMapper.deleteByPrimaryKey(cno);
            //删除老师课程
            Example example1 = new Example(TeacherCourse.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("cno",cno);
            int i2 = teacherCourseMapper.deleteByExample(example1);
            if (i1>0 && i2>0){
                return new ResultVo(1000,"success",null);
            }
        }
        return new ResultVo(1001,"error",null);
    }


}
